<template>
    <div class="sidebar">
        <el-menu
            class="sidebar-el-menu"
            :default-active="onRoutes"
            :collapse="collapse"
            text-color="#929498"
            active-text-color="#FFFFFF"
            unique-opened
            router
        >
            <template v-for="item in items">
                <template v-if="item.subs">
                    <el-submenu :index="item.index" :key="item.index">
                        <template slot="title">
                            <i :class="item.icon" class="icon-style"></i>
                            <span slot="title" class="sidebar-submenu">{{ item.title }}</span>
                        </template>
                        <!-- 二级路由 -->
                        <template v-for="subItem in item.subs">
                            <el-submenu
                                v-if="subItem.subs"
                                :index="subItem.index"
                                :key="subItem.index"
                             >
                                <template slot="title">{{ subItem.title }}</template>

                                <!-- 三级路由 -->
                                <el-menu-item
                                    v-for="(threeItem,i) in subItem.subs"
                                    :key="i"
                                    :index="threeItem.index"
                                >{{ threeItem.title }}
                                </el-menu-item>
                            </el-submenu>
                            <!-- 二级路由菜单 -->
                            <el-menu-item
                                v-else
                                :index="subItem.index"
                                :key="subItem.index"
                                class="tow-sub-menu-title"
                            >{{ subItem.title }}</el-menu-item>
                        </template>
                    </el-submenu>
                </template>
                <!-- 只有一级路由 -->
                <template v-else>
                    <el-menu-item :index="item.index" :key="item.index">
                        <i :class="item.icon" class="icon-style"></i>
                        <span slot="title" class="sidebar-submenu">{{ item.title }}</span>
                    </el-menu-item>
                </template>
            </template>
        </el-menu>
    </div>
</template>

<script>
// 图标库
import "@/assets/css/iconFont/iconfont.css";

import bus from '../common/bus';
export default {
    data() {
        return {
            collapse: false,
            //items控制显示的菜单栏，通过后端获取
            items: [
                {
                    icon: 'iconfont icon-daohangshouye',
                    index: 'dashboard',
                    title: '首页'
                },
                {
                    icon: 'iconfont icon-wulianwang',
                    index: '2',
                    title: '物联网卡',
                    subs: [
                        {
                            index: 'netcardList',
                            title: '网卡列表'
                        }
                    ]
                },
                {
                    icon: 'iconfont icon-shebeiguanli',
                    index: '3',
                    title: '设备管理',
                    subs: [
                        {
                            index: 'deviceList',
                            title: '设备列表'
                        },
                        {
                            index: 'deviceGroupList',
                            title: '磅组列表'
                        }
                    ]
                },
                {
                    icon: 'iconfont icon-jibenxinxi',
                    index: '4',
                    title: '基本信息',
                    subs: [
                        {
                            index: 'dataSourceList',
                            title: '数据中心'
                        },
                        {
                            index: 'stationList',
                            title: '站点列表'
                        }
                    ]
                },
                {
                    icon: 'iconfont icon-jihuaguanli',
                    index: '5',
                    title: '计划管理',
                    subs: [
                        {
                            index: 'PlanUploadList',
                            title: '计划上传'
                        },
                        {
                            index: 'PlanConfigurationList',
                            title: '计划配置'
                        },
                        {
                            index: 'SyncTableList',
                            title: '同步表管理'
                        }
                    ]
                },
                {
                    icon: 'iconfont icon-jianzhuanquan',
                    index: '6',
                    title: '报警中心',
                    // subs: [
                        // {
                        //     index: 'deviceList',
                        //     title: '设备列表'
                        // }
                    // ]
                },
                {
                    icon: 'iconfont icon-liucheng',
                    index: '7',
                    title: '流程设计',
                    subs: [
                        {
                            index: 'businessList',
                            title: '业务列表'
                        }
                    ]
                }
            ]
        };
    },
    computed: {
        onRoutes() {
            return this.$route.path.replace('/', '');
        }
    },
    created() {
        // 通过 Event Bus 进行组件间通信，来折叠侧边栏
        bus.$on('collapse', msg => {
            this.collapse = msg;
            bus.$emit('collapse-content', msg);
        });
    }
};
</script>

<style scoped>
/* 图标 */
.icon-style{
    width: 20px;
    height: 20px;
    font-size: 20px;
}

.sidebar {
    font-size: 18px;
    display: block;
    position: absolute;
    width: 200px;
    left: 0;
    top: 70px;
    bottom: 0;
    overflow-y: scroll;
    /* text-align: center; */
}
/* 激活后的子菜单项 */
.el-menu-item.is-active{
    /* background-color: #745CF2; */
    background-color: #C4B0FD;
    color: #FFFFFF !important;
}
.sidebar .sidebar-submenu{
    /* width: 71px; */
    height: 18px;
    font-size: 18px;
    font-family: Source Han Sans SC;
    font-weight: 400;
    margin-left: 12px;
    /* color: #929498; */
    line-height: 13px;

}
/* 二级菜单 */
.tow-sub-menu-title{
    font-size: 16px;
    padding-left: 50px !important;
}
.sidebar::-webkit-scrollbar {
    width: 0;
}
.sidebar-el-menu{
    margin-top: 20px;
}

.sidebar-el-menu:not(.el-menu--collapse) {
    width: 200px;
}
.sidebar > ul {
    height: 100%;
}
/* .sidebar > ul > li{
    padding-left: 53px !important;
} */
</style>
